<template>
  <div class="mod-config">
    <div
      v-loading="loadingPage"
      class="search-form search_form"
    >
      <el-form
        :model="searchForm"
        size="mini"
        label-position="right"
        class="demo-form-inline"
        label-width="110px"
      >
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="部门名称：">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="6"
            style="text-align:center;"
          >
            <el-button
              size="mini"
              type="primary"
              @click="searchBtn"
            >查询
            </el-button>
            <el-button
              size="mini"
              @click="emptyClick"
            >清空
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>

      <el-col :span="8" style="text-align:left;margin-bottom: 10px;">
        <el-button size="mini" type="primary" @click="addOrUpdateHandle()"
        >新增
        </el-button>
      </el-col>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      :row-key="bindRowKeys"
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        prop="z00hrjgmc"
        header-align="center"
        align="center"
        label="部门名称"
      />
      <el-table-column
        prop="z00hrjgbm"
        header-align="center"
        align="center"
        label="部门编号"
        width="160"
      />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.number)"
          >修改
          </el-button>

          <el-button
            type="text"
            size="small"
            @click="deleteDept(scope.row.number)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="progress_lok_page">
      <page
        :hidden="tableData && tableData.length === 0"
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="handlePageChange"
      />
    </div>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></add-or-update>
  </div>

</template>

<script>

  import axios from 'axios'
  import AddOrUpdate from './deptInfo-add-or-update'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        list: [],
        tableData: [],
        loadingPage: true,
        addOrUpdateVisible: false,
        loading: true,
        currentPage: 0,
        page: 1,
        pageSize: 10,
        total: 0,
        searchForm: {
          name: '' // 搜索部门名称
        }
      }
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getList ()
    },

    methods: {
      // 获取列表
      getList() {
        var uu = {
          page: this.page,
          pageSize: this.pageSize,
          name: this.searchForm.name
        }
        var jsonString = JSON.stringify (uu)
        var yy = Encrypt (jsonString)
        var encodedString = encodeURIComponent (yy)
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/torgdept/page',
          data: encodedString
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.tableData = data.content
            this.total = data.total
            this.loading = false
            this.loadingPage = false
          } else {
            this.loading = false
            this.loadingPage = false
            this.$message.error (data.msg)
            this.tableData = []
          }
        }).catch(error => {
          this.$message.error ('服务器访问异常，请从新登录')
        });
      },
      /** 点击分页触发的事件 */
      handlePageChange(pageInfo) {
        this.loading = true
        this.page = pageInfo.page
        this.pageSize = pageInfo.limit
        this.getList ()
      },
      // 清空
      emptyClick() {
        this.searchForm.name = ''
      },

      bindRowKeys(row) {
        return row.id
      },
      searchBtn() {
        this.loading = true
        this.page = 1
        this.getList ()
      },
      // 多选
      handleSelectionChange(value) {
        this.checkedArr = value
        // element表格多选第一页选中往后翻一页全选，再回到第一页会再次选中第一页的数据，所以需要手动去重
        const hash = {}
        this.checkedArr = this.checkedArr.reduce ((preVal, curVal) => {
          hash[curVal.id] ? '' : hash[curVal.id] = true && preVal.push (curVal)
          return preVal
        }, [])
      },

      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick (() => {
          this.$refs.addOrUpdate.init (id)
        })
      },
      // 删除
      deleteDept(id) {
        this.$confirm (`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then (() => {
          let list = new Array ()
          list.push (id)
          axios ({
            url: '/hr-xc-human-resources-perform-manage/generator/torgdept/batchDelete',
            method: 'post',
            data: list
          }).then (({ data }) => {
            if (data && data.code === '00000') {
              this.$message ({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getList ()
                }
              })
            } else {
              this.$message.error (data.msg)
            }
          }).catch(error => {
          this.$message.error ('服务器访问异常，请从新登录')
        });
        })
      }
    }
  }
</script>
